package cn.fz.mpdemo.controller;

import cn.fz.mpdemo.domain.po.EmpEntity;
import cn.fz.mpdemo.domain.query.EmpQuery;
import cn.fz.mpdemo.domain.request.EmpRequest;
import cn.fz.mpdemo.domain.vo.PageVO;
import cn.fz.mpdemo.domain.vo.ResultVO;
import cn.fz.mpdemo.service.EmpService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 员工表 前端控制器
 * </p>
 *
 * @author gecy
 * @since 2024-07-06 14:24:14
 */
@RestController
@RequestMapping("/emp")
@Tag(name = "员工模块接口",description = "员工模块接口")
@RequiredArgsConstructor
public class EmpController {
    private final EmpService empService;

    @Operation(summary = "获取所有员工")
    @GetMapping("getAll")
    public List<EmpEntity> getAll() {
        return empService.list();
    }

    @Operation(summary = "获取request请求的数据")
    @GetMapping("getReqInfo")
    public ResultVO<Object> getReqInfo(HttpServletRequest request){
        Object userInfo = request.getAttribute("userInfo");
        return ResultVO.success(userInfo);
    }

    @Operation(summary = "分页查询员工")
    @PostMapping("queryPage")
    public ResultVO<PageVO<EmpEntity>> queryPage(@RequestBody EmpQuery query) {
        PageVO<EmpEntity> pageVO = empService.queryPage(query);
        return ResultVO.success(pageVO);
    }

    @Operation(summary = "上传excel")
    @PostMapping("upload")
    public ResultVO<List<Map<String,String>>> upload(@RequestParam("file") MultipartFile file) {
        List<Map<String,String>> list = empService.readExcelFile(file);
        return ResultVO.success(list);
    }

    @Operation(summary = "excel导出")
    @PostMapping("export")
    public void export(@RequestBody EmpQuery query, HttpServletResponse response) {
        empService.export(query,response);
    }

    @Operation(summary = "新增部门和员工")
    @PostMapping("saveEmp")
    public ResultVO<String> saveEmp(@RequestBody EmpRequest request) {
        empService.saveEmp(request);
        return ResultVO.success("ok");
    }
}
